package cn.kgc.crowdfundingadmin.service;

import cn.kgc.crowdfundingadmin.common.CrowdFundingUtils;
import cn.kgc.crowdfundingadmin.mapper.AdminMapper;
import cn.kgc.crowdfundingadmin.pojo.Admin;
import cn.kgc.crowdfundingadmin.pojo.AdminExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author shkstart
 * @create 2020-02-19 15:14
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Resource
    AdminMapper adminMapper;
    AdminService adminService;
    @Override
    public Admin selectById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);
    }
    @Override
    public Admin login(String loginAcct, String userPswd) {
        Admin admin=null;
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria=example.createCriteria();
        criteria.andLoginacctEqualTo(loginAcct);
        List<Admin> list=adminMapper.selectByExample(example);
        if (CrowdFundingUtils.collectionEffective(list)) {
            admin=list.get(0);
            String md5=CrowdFundingUtils.md5(userPswd);
            if(md5.equals(admin.getUserpswd())){
                return admin;
            }else{
                admin=null;
            }
        }
        return admin;
    }

    @Override
    public PageInfo<Admin> queryForKeywordSearch(Integer pageNum, Integer pageSize, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("id desc");
        if(CrowdFundingUtils.stringEffective(keyword)){
            AdminExample example =new AdminExample();
            AdminExample.Criteria criteria1= example.createCriteria();
            criteria1.andCreatetimeLike("%"+keyword+"%");
            AdminExample.Criteria criteria2= example.createCriteria();
            criteria2.andUsernameLike("%"+keyword+"%");
            AdminExample.Criteria criteria3= example.createCriteria();
            criteria3.andEmailLike("%"+keyword+"%");

            example.or(criteria2);
            example.or(criteria3);

            List<Admin>list=adminMapper.selectByExample(example);
            PageInfo <Admin> pageInfo=new PageInfo<>(list);
            return pageInfo;
        }else {
            List<Admin>list=adminMapper.selectByExample(null);
            PageInfo <Admin> pageInfo=new PageInfo<>(list);
            return pageInfo;
        }

    }

    @Override
    public void batchRemove(List<Integer> adminIdList) {
        AdminExample example=new AdminExample();
        AdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdIn(adminIdList);

        adminMapper.deleteByExample(example);
    }

    @Override
    public void saveAdmin(Admin admin) {
        String userPswd = admin.getUserpswd();
        userPswd = CrowdFundingUtils.md5(userPswd);
        admin.setUserpswd(userPswd);

        // 执行保存
        adminMapper.insert(admin);
    }

    @Override
    public Admin getAdminById(Integer adminId) {
        return adminMapper.selectByPrimaryKey(adminId);
    }

    @Override
    public void updateAdmin(Admin admin) {
        // 对密码进行加密
        if (CrowdFundingUtils.stringEffective(admin.getUserpswd())){
            String s=CrowdFundingUtils.md5(admin.getUserpswd());
            admin.setUserpswd(s);
        }
        adminMapper.updateByPrimaryKeySelective(admin);
    }
}
